﻿/*  ----------------------------------------------------------------------------
 *  Project Intercept
 *  ----------------------------------------------------------------------------
 *  System.Common
 *  ----------------------------------------------------------------------------
 *  File:        Strings.cs
 *  Authors:     Dremation,Vendetta
 *  ----------------------------------------------------------------------------
 *  GNU General Public License version 2 (GPLv2)
 */

using System.Text.RegularExpressions;

namespace System.Common
{
    public class Strings
    {
        public static bool IsNumber(string strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&
            !objTwoDotPattern.IsMatch(strNumber) &&
            !objTwoMinusPattern.IsMatch(strNumber) &&
            objNumberPattern.IsMatch(strNumber);            
        }

        public static bool IsAlpha(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }

        public static bool IsAlphaNumeric(string strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }

        public static bool IsNaturalNumber(String strNumber)
        {
            Regex objNotNaturalPattern = new Regex("[^0-9]");
            Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");
            return !objNotNaturalPattern.IsMatch(strNumber) && objNaturalPattern.IsMatch(strNumber);
        }

        public static bool IsWholeNumber(string strNumber)
        {
            Regex objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strNumber);
        }

        public static bool IsInteger(string strNumber)
        {
            Regex objNotIntPattern = new Regex("[^0-9-]");
            Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
            return !objNotIntPattern.IsMatch(strNumber) &&
            objIntPattern.IsMatch(strNumber);
        }

        public static bool IsPositiveNumber(string strNumber)
        {
            Regex objNotPositivePattern = new Regex("[^0-9.]");
            Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            return !objNotPositivePattern.IsMatch(strNumber) &&
            objPositivePattern.IsMatch(strNumber) &&
            !objTwoDotPattern.IsMatch(strNumber);
        }


        //Vendetta's functions, 3/28/2010
        /// <summary>
        /// Checks to see if the specified string consists of all lower case characters
        /// </summary>
        /// <param name="ToBeChecked">The string in question</param>
        /// <returns>Returns true if all characters in the string are lower case, false if one or more character is not lowercase</returns>
        public static bool IsLowerCase(string ToBeChecked)
        {
            System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex("[^a-z]");
            return !pattern.IsMatch(ToBeChecked);
        }
        /// <summary>
        /// Checks if the specified string consists of all upper case characters
        /// </summary>
        /// <param name="ToBeChecked">The string to be checked</param>
        /// <returns>Returns true if the string is all uppercase, false otherwise</returns>
        public static bool IsUppercase(string ToBeChecked)
        {
            System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex("[^A-Z]");
            return !pattern.IsMatch(ToBeChecked);
        }
        /// <summary>
        /// Generates a random string
        /// </summary>
        /// <param name="strLength">The length of the string</param>
        /// <param name="Alphaonly">Whether the string should consist of A-Z only (If false it will include 0-9)</param>
        /// <param name="UpperCase">If the returned string should be uppercase or lowercase</param>
        /// <returns>Returns a string with random characters (and numbers if specified)</returns>
        public static string GenerateRandomString(int strLength, bool Alphaonly, bool UpperCase)
        {
            StringBuilder b = new StringBuilder();
            Random ran = new Random();
            for (int i = 0; i < strLength; i++)
            {
                // check if Alphaonly and if not generate a random num to see if we should randomly get an int or a char...
                char theChar = (!Alphaonly && ran.Next(0, 10) % 2 == 0 ? ran.Next(0, 9).ToString()[0] : Convert.ToChar(Convert.ToInt32(Math.Floor(26 * ran.NextDouble() + 65))));
                if (!UpperCase)
                    theChar = Convert.ToChar(theChar.ToString().ToLower());

                b.Append(theChar);
            }
            return b.ToString();
        }
    }
}
